<?php
/**
 * Created by PhpStorm.
 * User: Lxx<779219930@qq.com>
 * Date: 2017/2/17
 * Time: 16:28
 */

namespace app\home\controller;

use think\Controller;
use app\home\model\WechatUser;
use app\admin\model\Sign;
use app\home\model\Summary;
use app\home\model\Test;
use think\Db;

class Count extends Controller {
    /**
     * 每天定时查询签到比 和排名
     */
    public function index()
    {
        ini_set('max_execution_time', '600');
        $Users = WechatUser::where(['subscribe' => 1, 'review_status' => 1,'role' => 0])->select();
        $temp = array(); // 每日签到
        $temps = array(); // 每日总结
        $test = array();  // 测试
        if (date('m') == 1){
            // 当年的第一个月
            $m = 12;
            $y = date('Y')-1;
        }else{
            $m = date('m')-1;
            $y = date('Y');
        }
        //  上月
        $before = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
        $now = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d').'day')));
        $day = cal_days_in_month(CAL_GREGORIAN,$m, $y);  // 获取上个月 有多少天
        $n = 0;  // 周一的个数
        for ($i = 1; $i <= $day; $i++) {
            $time = mktime(10, 0, 0, $m, $i, $y);
            if (date("N", $time) == 1) {
                $n++;
            }
        }
        $temp_p = []; // 签到 百分比
        $temp_d = [];  // 签到 方差
        $temp_e = [];  // 签到 均值
        $temps_p = [];  // 总结 百分比
        $temps_d = [];  // 总结  方差
        $temps_e = [];  // 总结  均值
        foreach ($Users as $key => $value) {
            // 测试
            $scores1 = Test::where(['openid' => $value['openid'], 'status' => 0, 'test_time' => ['between', [$before, $now]]])->column('score');    // 每个值
            $sum_test = count($scores1);  // 测试总个数
            $test_dx = 999.9;
            $test_ex = 0;
            if ($sum_test != 0) {
                $num_test = array_sum($scores1);  // 分数总和
                $test_ex = $num_test / $sum_test; // 均值
                $stability_test = 0;
                foreach ($scores1 as $val) {
                    $tem = $val - $test_ex;
                    $stability_test += pow($tem, 2);
                }
                $test_dx = round($stability_test / $sum_test, 2);
            }
            // 签到
            $sum_sign = $day - $n;  // 总天数
            $scores2 = Sign::where(['openid' => $value['openid'], 'create_time' => ['between', [$before, $now]]])->column('create_time');  // 每个值
            $time_sign = count($scores2);  // 签到总个数
            $percent = round(($time_sign / $sum_sign), 3);   // 签到比
            if ($percent > 1) {
                $percent = 1;
            }
            $temp[$key]['percent'] = $percent;
            $sign_dx = 999.9;
            $sign_ex = "00:00";
            $sign_e = 0;
            if ($time_sign != 0) {
                $time1 = 0;
                $time2 = 0;
                foreach ($scores2 as $val) {
                    // 时间戳总和
                    $time1 += date('H', $val);
                    $time2 += date('i', $val);
                }
                // 平均值
                $average2_h = floor($time1 / $time_sign);
                $average2_i = round($time2 / $time_sign);
                // 方差
                $stability2 = 0;
                foreach ($scores2 as $val) {
                    // 时间戳总和
                    $tem = (date('H', $val)+date('i',$val)/100) - ($average2_h+$average2_i/100);
                    $stability2 += pow($tem, 2);
                }
                $sign_dx = round($stability2 / $time_sign, 2);
                if ($average2_i < 10){
                    $average2_i = "0".$average2_i;
                }
                $sign_ex = $average2_h.":".$average2_i;
                $sign_e = $average2_h + $average2_i/100;
            }
            $temp[$key]['sign_ex'] = $sign_ex;
            $temp[$key]['sign_dx'] = $sign_dx;
            $temp[$key]['sign_e'] = $sign_e;
            $temp[$key]['sign_s'] = $time_sign;
            $temp_p[] = $percent;
            $temp_d[] = $sign_dx;
            $temp_e[] = $sign_e;
            // 每日总结
            $scores3 = Summary::where(['openid' => $value['openid'], 'create_time' => ['between', [$before, $now]]])->column('create_time');   // 每个值
            $times = count($scores3);
            $percents = round(($times / ($day - $n)), 3);   // 总结比
            if ($percents > 1) {
                $percents = 1;
            }
            $summary_dx = 999.9;
            $summary_ex = "00:00";
            $summary_e = 0;
            if ($times != 0) {
                $time3 = 0;
                $time4 = 0;
                foreach ($scores3 as $val) {
                    // 时间戳总和
                    $time3 += date('H', $val);
                    $time4 += date('i', $val);
                }
                // 平均值
                $average3_h = floor($time3 / $times);
                $average3_i = round($time4 / $times);
                // 方差
                $stability3 = 0;
                foreach ($scores3 as $val) {
                    // 时间戳总和
                    $tem = (date('H', $val)+date('i',$val)/100) - ($average3_h+$average3_i/100);
                    $stability3 += pow($tem, 2);
                }
                $summary_dx = round($stability3 / $times, 2);
                if ($average3_i < 10){
                    $average3_i = "0".$average3_i;
                }
                $summary_ex = $average3_h.":".$average3_i;
                $summary_e = $average3_h + $average3_i/100;
            }
            $temps[$key]['percents'] = $percents;
            $temps_p[] = $percents;
            $temps_d[] = $summary_dx;
            $temps_e[] = $summary_e;
            $temps[$key]['summary_dx'] = $summary_dx;
            $temps[$key]['summary_ex'] = $summary_ex;
            $temps[$key]['summary_e'] = $summary_e;
            $temps[$key]['summary_s'] = $times;
            $temp[$key]['openid'] = $value['openid'];
            $temp[$key]['name'] = $value['name'];
            $temp[$key]['school_id'] = $value['school_id'];
            $temp[$key]['order_id'] = $value['order_id'];
            $temps[$key]['openid'] = $value['openid'];
            $temps[$key]['name'] = $value['name'];
            $temps[$key]['school_id'] = $value['school_id'];
            $temps[$key]['order_id'] = $value['order_id'];
            $test[$key]['openid'] = $value['openid'];
            $test[$key]['name'] = $value['name'];
            $test[$key]['school_id'] = $value['school_id'];
            $test[$key]['order_id'] = $value['order_id'];
            $test[$key]['test_ex'] = $test_ex;
            $test[$key]['test_dx'] = $test_dx;
            $test[$key]['test_s'] = $sum_test;
        }
        array_multisort($temp_p, SORT_DESC,$temp_d, SORT_ASC, $temp_e,SORT_ASC,$temp);//多维数组的排序
        $table = "info".$y;
        foreach ($temp as $key => $value) {
            $res = Db::name($table)->where(['openid' => $value['openid'],'mouth' => $m])->find();
            if ($res){
                Db::name($table)->where(['id' => $res['id']])->update(['name' => $value['name'],'school_id' => $value['school_id'],'order_id' => $value['order_id'],'sign_s' => $value['sign_s'],'sign_p' => $value['percent'],'sign_rank' => $key+1,'sign_ex' => $value['sign_ex'], 'sign_dx' => $value['sign_dx']]);
            }else{
                Db::name($table)->insert(['openid' => $value['openid'],'name' => $value['name'],'school_id' => $value['school_id'],'sign_s' => $value['sign_s'],'mouth' => $m,'order_id' => $value['order_id'],'sign_p' => $value['percent'],'sign_rank' => $key+1,'sign_ex' => $value['sign_ex'], 'sign_dx' => $value['sign_dx']]);
            }
        }
        array_multisort($temps_p, SORT_DESC, $temps_d, SORT_ASC,$temps_e,SORT_ASC,$temps);//多维数组的排序
        foreach ($temps as $key => $value) {
            $res = Db::name($table)->where(['openid' => $value['openid'],'mouth' => $m])->find();
            if ($res){
                Db::name($table)->where(['id' => $res['id']])->update(['name' => $value['name'],'school_id' => $value['school_id'],'order_id' => $value['order_id'],'summary_s' => $value['summary_s'],'summary_rank' => $key+1,'summary_p' => $value['percents'],'summary_ex' => $value['summary_ex'], 'summary_dx' => $value['summary_dx']]);
            }else{
                Db::name($table)->insert(['openid' => $value['openid'],'name' => $value['name'],'school_id' => $value['school_id'],'summary_s' => $value['summary_s'],'mouth' => $m,'order_id' => $value['order_id'],'summary_p' => $value['percents'],'summary_rank' => $key+1,'summary_ex' => $value['summary_ex'], 'summary_dx' => $value['summary_dx']]);
            }
        }
        array_multisort(array_column($test,'test_dx'), SORT_ASC, array_column($test,'test_ex'), SORT_DESC,$test);//多维数组的排序
        foreach ($test as $key => $value) {
            $res = Db::name($table)->where(['openid' => $value['openid'],'mouth' => $m])->find();
            if ($res){
                Db::name($table)->where(['id' => $res['id']])->update(['name' => $value['name'],'school_id' => $value['school_id'],'test_s' => $value['test_s'],'order_id' => $value['order_id'],'test_rank' => $key + 1,'test_ex' => $value['test_ex'], 'test_dx' => $value['test_dx']]);
            }else{
                Db::name($table)->insert(['openid' => $value['openid'],'name' => $value['name'],'school_id' => $value['school_id'],'test_s' => $value['test_s'],'mouth' => $m,'order_id' => $value['order_id'],'test_rank' => $key + 1,'test_ex' => $value['test_ex'], 'test_dx' => $value['test_dx']]);
            }
        }
    }
}